//获取应用实例
const app = getApp()

Page({
  data: {
    //imgUrl
    imgUrl: app.globalData.iconImgUrl,
    agree: true, //同意列表是否显示
    noExamine: true, //  是否显示未审核面板
    StatusBar: app.globalData.StatusBar, //状态栏高度
    CustomBar: app.globalData.CustomBar, //头部nav高度
    backIcon: true, // 返回按钮是否显示
    backImg: app.globalData.iconImgUrl + "arrow-left.svg",
    title: "审核", //页面标题
    // 审核未审核对象
    navItem: [{
        name: "未审核",
        number: 0,
        active: "active"
      },
      {
        name: "已审核",
        number: 0,
        active: ""
      }
    ],
    //未审核列表对象
    list: [],
    // 已审核通过列表对象
    agreeList: [],
    // 已审核未通过列表对象
    noAgreeList: [],
    // 同意不同意数据对象
    buttonTabList: [{
        name: "同意",
        active: "active"
      },
      {
        name: "不同意",
        active: ""
      }
    ],
    nototalResult: 0,
    nowIndex: 0,
    type: 0,
    currentPage: 1,
    pageSize: 10,
    totolPage: 1,
    agreeCurrentPage: 1,
    agreePageSize: 10,
    agreeTotolPage: 1,
    noAgreeCurrentPage: 1,
    noAgreePageSize: 10,
    noAgreeTotolPage: 1,
  },
  onShow() {
    this.setData({
      list: [],
      agreeCurrentPage: 1,
      agreeTotolPage: 1,
      agreeList: [],
      noAgreeCurrentPage: 1,
      noAgreeTotolPage: 1,
      noAgreeList: [],
      noAgreeCurrentPage: 1,
      noAgreeTotolPage: 1,
    })
    this.getLIst()
    this.getAgreeList()
    this.getNoAgreeList()
  },
  // 获取未审核列表数据
  getLIst() {
    var that = this

    wx.request({
      url: app.globalData.baseUrl + 'application/getsh.do',
      data: {
        type: '0',
        userCode: wx.getStorageSync('userCode'),
        pageSize: that.data.pageSize,
        currentPage: that.data.currentPage
      },
      method: 'post',
      success(res) {
        // console.log('获取为未审核列表数据', res)
        const historyList = that.data.list
        const requestList = res.data.datas
        var list = that.pushData(historyList, requestList)
        // console.log('listData', list)
        that.setData({
          list: list,
          // ['navItem[0].number']: res.data.pageInfo.totalResult,
          // totalPage: res.data.pageInfo.totalPage
          ['navItem[0].number']: res.data.page.totalResult,
          totalPage: res.data.page.totalPage
        })
      }
    })
  },
  // 获取同意审核列表
  getAgreeList() {
    var that = this

    wx.request({
      url: app.globalData.baseUrl + 'application/getsh.do',
      data: {
        type: 1,
        userCode: wx.getStorageSync('userCode'),
        pageSize: that.data.agreePageSize,
        currentPage: that.data.agreeCurrentPage
      },
      method: 'post',
      success(res) {
        console.log('同意审核列表', res)
        const historyList = that.data.agreeList
        const requestList = res.data.datas
        var list = that.pushData(historyList, requestList)
        that.setData({
          agreeList: list,
          ['navItem[1].number']: res.data.page.totalResult,
          agreeTotolPage: res.data.page.totalPage
        })
      }
    })
  },
  // 获取未同意审核列表
  getNoAgreeList() {
    var that = this

    wx.request({
      url: app.globalData.baseUrl + 'application/getsh.do',
      data: {
        type: -1,
        userCode: wx.getStorageSync('userCode'),
        pageSize: that.data.noAgreePageSize,
        currentPage: that.data.noAgreeCurrentPage
      },
      method: 'post',
      success(res) {
        console.log('未同意审核列表', res)
        const historyList = that.data.noAgreeList
        const requestList = res.data.datas
        var list = that.pushData(historyList, requestList)
        that.setData({
          noAgreeList: list,
          noAgreeTotolPage: res.data.page.totalPage
        })
      }
    })
  },
  pushData(srcData, oprateData) {
    var tempArr = srcData
    oprateData.forEach(item => {
      tempArr.push(item)
    })
    return tempArr
  },
  // 跳转到审核表单页面
  toExamine(e) {
    var id = e.currentTarget.dataset.id
    // var approve = wx.getStorageSync("approve")

    wx.navigateTo({
      url: './examine?id=' + id,
    })

  },
  // 跳转到审核详情页面
  toDetail() {
    wx: wx.navigateTo({
      url: '../examineDetail/examine',
      success: function (res) {},
      fail: function (res) {},
      complete: function (res) {},
    })
  },
  //审核未审核切换事件
  navTap(e) {
    var index = e.target.dataset.index,
      item = 'navItem[' + index + '].active',
      item2,
      noExamine,
      type = this.data.type;

    var addtion;

    if (index === 0) {
      item2 = 'navItem[1].active'
      noExamine = true;
      type = 0;
      addtion = {
        list: [],
        currentPage: 1,
        totolPage: 1
      }
      this.setData({
        [item]: "active",
        [item2]: "",
        noExamine: noExamine,
        nowIndex: index,
        type: type,
        ...addtion
      })
      this.getLIst()
    } else {
      item2 = 'navItem[0].active'
      noExamine = false;
      type = 1
      addtion = {
        agreeList: [],
        agreeCurrentPage: 1,
        agreeTotolPage: 1
      }
      this.setData({
        [item]: "active",
        [item2]: "",
        noExamine: noExamine,
        nowIndex: index,
        type: type,
        ...addtion
      })
      this.getAgreeList()
    }
  },
  //同意不同意tab切换事件
  buttonTabTap(e) {
    var index = e.target.dataset.index,
      item = 'buttonTabList[' + index + '].active',
      item2,
      agree,
      type = this.data.type;

    var addtion;

    if (index === 0) {
      item2 = 'buttonTabList[' + 1 + '].active'
      agree = true;
      type = 1;
      addtion = {
        nowIndex: 1,
        agreeList: [],
        agreeCurrentPage: 1,
        agreeTotolPage: 1
      }
      this.setData({
        [item]: "active",
        [item2]: "",
        agree: agree,
        type: type,
        ...addtion
      })
      this.getAgreeList()
    } else {
      item2 = 'buttonTabList[' + 0 + '].active'
      agree = false;
      type = -1
      addtion = {
        nowIndex: 2,
        noAgreeList: [],
        noAgreeCurrentPage: 1,
        noAgreeTotolPage: 1
      }
      this.setData({
        [item]: "active",
        [item2]: "",
        agree: agree,
        type: type,
        ...addtion
      })
      this.getNoAgreeList()
    }


  },
  onPullDownRefresh() {
    const nowIndex = this.data.nowIndex
    const that = this
    //wx.showNavigationBarLoading() //在标题栏中显示加载
    wx.showLoading({
      title: '刷新中...',
    })

    switch (nowIndex) {
      case 0:
        that.setData({
          list: [],
          currentPage: 1,
          totolPage: 1
        })
        that.getLIst()

        break;
      case 1:
        that.setData({
          agreeList: [],
          agreeCurrentPage: 1,
          agreeTotolPage: 1
        })
        that.getAgreeList()

        break;
      case 2:
        that.setData({
          noAgreeList: [],
          noAgreeCurrentPage: 1,
          noAgreeTotolPage: 1
        })
        that.getNoAgreeList()
        break;
      default:
        break;

    }
    setTimeout(function () {

      //wx.hideNavigationBarLoading() //完成停止加载
      wx.hideLoading()
      wx.stopPullDownRefresh() //停止下拉刷新

    }, 1500);
  },
  onReachBottom() {
    const type = this.data.type
    const that = this

    switch (type) {
      case 0:
        const currentPage = that.data.currentPage
        const totalPage = that.data.totalPage
        console.log(currentPage, totalPage)
        if (currentPage < totalPage) {
          that.setData({
            currentPage: currentPage + 1
          })
          that.getLIst()
        }
        break
      case 1:
        const agreeCurrentPage = that.data.agreeCurrentPage
        const agreeTotolPage = that.data.agreeTotolPage
        if (currentPage < totalPage) {
          that.setData({
            agreeCurrentPage: agreeCurrentPage + 1
          })
          that.getAgreeList()
        }
        break
      case -1:
        const noAgreeCurrentPage = that.data.noAgreeCurrentPage
        const noAgreeTotolPage = that.data.noAgreeTotolPage
        if (currentPage < totalPage) {
          that.setData({
            noAgreeCurrentPage: noAgreeCurrentPage + 1
          })
          that.getNoAgreeList()
        }
        break
      default:
        break
    }
  }
})